package com.xc.freeapp.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xc.freeapp.dao.VaccineDao;
import com.xc.freeapp.dao.VaccinesMapper;
import com.xc.freeapp.dao.dataSource.DBContextHolder;
import com.xc.freeapp.entity.Vaccine;
import com.xc.freeapp.entity.Vaccines;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.VaccinesService;
import com.xc.freeapp.util.Constant;
@Service
public class VaccinesServiceImpl implements VaccinesService{
	private Logger log = Logger.getLogger(this.getClass());
	@Autowired
	VaccineDao vaccineDao;
	@Autowired
	VaccinesMapper  vaccinesMapper;
	/**
	 * 
	 */
	public List<Vaccines> getVaccinesList(Integer vaccineperid) throws BaseException{
		DBContextHolder.switchDataSourceRW();//切换数据源
		return vaccinesMapper.selectList(vaccineperid);
	}
	/**
	 * 
	 * @param vaccinePerId
	 * @param vaccineType
	 * @param ifdo
	 * @throws BaseException
	 */
	public void updateifdo(Integer vaccinePerId,Integer vaccineType,Integer ifdo)throws BaseException{
		DBContextHolder.switchDataSourceRW();//切换数据源
		try{
			Vaccine vaccine=vaccineDao.selectByPerIdAndType(vaccinePerId, vaccineType);
			if(vaccine==null){
				Vaccine nvaccine=new Vaccine();
				nvaccine.setVaccineperid(vaccinePerId);
				nvaccine.setVaccinetype(vaccineType);
				nvaccine.setIfdo(1);
				vaccineDao.insert(nvaccine);
			}else{
				vaccine.setIfdo(ifdo);
				vaccineDao.updateByPrimaryKey(vaccine);
			}
		}catch(Exception e){
			e.printStackTrace();
			log.error("更新是否接种疫苗失败"+e);
			throw new BaseException(Constant.GLOBAL_ERROR);
		}
	}
	

}
